package com.gitee.swsk33.dbproduct.service.impl;

import com.gitee.swsk33.dbentity.dataobject.Product;
import com.gitee.swsk33.dbentity.model.Result;
import com.gitee.swsk33.dbfeign.feignclient.OrderClient;
import com.gitee.swsk33.dbproduct.dao.ProductDAO;
import com.gitee.swsk33.dbproduct.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDAO productDAO;

	@Autowired
	private OrderClient orderClient;

	@Override
	public Result<Product> getById(int id) {
		Result<Product> result = new Result<>();
		// 先查询商品
		Product getProduct = productDAO.selectById(id);
		if (getProduct == null) {
			result.setResultFailed("查询失败！");
			return result;
		}
		// 远程调用订单模块，查询与其关联的订单（多对多关联查询）
		List<Integer> orderIds = productDAO.selectOrderIds(id);
		getProduct.setOrders(orderClient.getByBatchId(orderIds).getData());
		result.setResultSuccess("查询成功！", getProduct);
		return result;
	}

	@Override
	public Result<List<Product>> getByBatchId(List<Integer> ids) {
		Result<List<Product>> result = new Result<>();
		// 先批量查询
		List<Product> products = productDAO.selectBatchIds(ids);
		// 由于批量查询目前专门提供给内部模块作为多对多关联查询时远程调用，因此这里不再对每个对象进行多对多查询，否则会陷入死循环
		result.setResultSuccess("查询成功！", products);
		return result;
	}

}